<?php

/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2016/3/25
 * Time: 19:31
 */
class Users_Model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

//    查询用户列表


    public function get_users($data){
        $sql="select * from user where is_deleted='N'";
        if(!empty($data['username'])){
            $sql=$sql." and username='".$data['username']."'";
        }
        if(!empty($data['password'])){
            $sql=$sql." and password='".$data['password']."'";
        }
        if(!empty($data['email'])){
            $sql=$sql." and email='".$data['email']."'";
        }
        if(!empty($data['mobile'])){
            $sql=$sql." and mobile='".$data['mobile']."'";
        }
        if(!empty($data['role'])){
            $sql=$sql." and role='".$data['role']."'";
        }
        if(!empty($data['office'])){
            $sql=$sql." and office='".$data['office']."'";
        }
        if(!empty($data['openid'])){
            $sql=$sql." and openid='".$data['openid']."'";
        }
        if(!empty($data['court'])){
            $sql=$sql." and court='".$data['court']."'";
        }
        if(!empty($data['is_verifyed'])){
            $sql=$sql." and is_verifyed='".$data['is_verifyed']."'";
        }
        if(!empty($data['sort'])){
            $sql=$sql." order by ".$data['sort'];
        }
        if(!empty($data['start_row'])){
            $sql=$sql." limit ".$data['start_row'].",".$data['number'];
        }

        $query=$this->db->query($sql);
        return $query->result_array();
    }

//    根据id查询用户
    public function get_user_by_id($id){
        $query = $this->db->get_where('user', array('id' => $id));
        $qr=$query->result_array();
        if($qr){
            return $qr[0];
        }else{
            return null;
        }
    }

    public function get_user_by_openid($openid){
        $sql="select * from user where is_deleted='N' and openid='".$openid."' limit 10";
        $arr=$this->db->query($sql);
        $result=$arr->result_array();
        return $result;
    }

//    新增用户
    public function add_user($data){
        $query=$this->db->insert('user',$data);
        $affectioned=$this->db->affected_rows();
        if($affectioned==1){
            return true;
        }else{
            return false;
        }
    }

//    删除文章
    public function delete_article_by_id($id){
        $this->db->set('is_deleted', 'Y');
        $this->db->set('gmt_modify', date("Y-m-d H:i:s" ,time()));
        $this->db->where('id', $id);
        $this->db->update('article');
        $affectiones=$this->db->affected_rows();
        if($affectiones>0){
            return true;
        }else{
            return false;
        }
    }

    public function update_user_by_id($user){
        $this->db->where('id', $user['id']);
        $this->db->update('user',$user);
        $affectlines= $this->db->affected_rows();
        if($affectlines>0){
            return true;
        }else{
            return false;
        }
    }

}